package service

import (
	"bgs/cache/redisImp"
	"bgs/conf"
	"bgs/db"
	"bgs/grpc/client"
	ledgerRunner "bgs/nsqp/runners/ledger"
	"bgs/util"
	"fmt"
	"go.uber.org/zap"
	"time"
)

// LedgerServiceImp ledger bizlogic
type LedgerServiceImp struct {
	dao          *db.PgDao
	slog         *zap.SugaredLogger
	wechatVars   *conf.WechatSection
	vars         *conf.VarsSection
	proxy        *client.GrpcClientProxy
	cacher       *cache.Adapter
	ledgerRunner *ledgerRunner.NsqLedgerRunner
}

// NewLedgerService initialization
func NewLedgerService(
	pgdao *db.PgDao,
	slog *zap.SugaredLogger,
	wechatVars *conf.WechatSection,
	vars *conf.VarsSection,
	proxy *client.GrpcClientProxy,
	adp *cache.Adapter,
	ledgerRunner *ledgerRunner.NsqLedgerRunner,
) *LedgerServiceImp {
	return &LedgerServiceImp{
		dao:          pgdao,
		slog:         slog.With("module", "LedgerService"),
		wechatVars:   wechatVars,
		vars:         vars,
		proxy:        proxy,
		cacher:       adp,
		ledgerRunner: ledgerRunner,
	}
}

// EnumProfitReceiverType ProfitReceiverType
type EnumProfitReceiverType string

const (
	// ProfitReceiverTypeMerchantID MERCHANT_ID
	ProfitReceiverType_MerchantID EnumProfitReceiverType = "MERCHANT_ID"

	// ProfitReceiverTypePERSONAL_OPENID PERSONAL_OPENID
	ProfitReceiverType_PERSONAL_OPENID EnumProfitReceiverType = "PERSONAL_OPENID"
)

// EnumProfitReceiverRelationType ProfitReceiverRelationType
type EnumProfitReceiverRelationType string

const (
	// ProfitReceiverRelationType_SUPPLIER SUPPLIER
	ProfitReceiverRelationType_SUPPLIER EnumProfitReceiverRelationType = "SUPPLIER"

	// ProfitReceiverRelationType_DISTRIBUTOR DISTRIBUTOR
	ProfitReceiverRelationType_DISTRIBUTOR EnumProfitReceiverRelationType = "DISTRIBUTOR"

	// ProfitReceiverRelationType_SERVICE_PROVIDER SERVICE_PROVIDER
	ProfitReceiverRelationType_SERVICE_PROVIDER EnumProfitReceiverRelationType = "SERVICE_PROVIDER"

	// ProfitReceiverRelationType_PLATFORM PLATFORM
	ProfitReceiverRelationType_PLATFORM EnumProfitReceiverRelationType = "PLATFORM"

	// ProfitReceiverRelationType_OTHERS OTHERS
	ProfitReceiverRelationType_OTHERS EnumProfitReceiverRelationType = "OTHERS"
)

// GenerateLedgerRequestSerialNumber 单号生成器
func (s *LedgerServiceImp) generateLedgerSerialNumber(prefix string, orderSerialNumber string) string {
	// LQ/LB(2)+订单的后(15)+(秒时间戳12)+随机数(3) = 32
	return fmt.Sprintf("%s%s%s%03d",
		prefix,
		orderSerialNumber[17:],
		util.GoTimeToStringf(time.Now(), "060102150405"),
		util.RandomInt(0, 999))
}
